<template>
  <FIModel title="导入流程" width="1000" v-model="showModel" @close="closeClick" @confirm="saveFunc">
    <el-input v-model="xmlData" style="width: 930px" :rows="15" type="textarea" placeholder="请输入" />
  </FIModel>
</template>
<script setup lang="ts">
import FIModel from '@/components/FIModel.vue'
import { ref, watch } from 'vue'
import { useMessage } from '@/hooks/message' // 通用message消息提示框

import { apiRequestPost } from '@/api/globalApi.js'
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  info: {
    type: Object,
    default: () => ({}),
  },
})
const emits = defineEmits(['close', 'update:visible', 'confirm', 'save-ok'])
const showModel = ref(props.visible)
const xmlData = ref('')
watch(
  () => props.visible,
  (val) => {
    if (showModel.value !== val) {
      showModel.value = val
    }
  }
)

watch(
  () => showModel.value,
  (val) => {
    // 只有值改变时才触发更新，避免无限循环
    if (val !== props.visible) {
      emits('update:visible', val)
    }
  }
)

const closeClick = () => {
  emits('update:visible', false)
  emits('close')
}

const saveFunc = () => {
  console.log('-------', props.info)
  if (!xmlData.value) {
    useMessage().warning('请输入流程数据')
    return
  }
  emits('update:visible', false)
  emits('confirm')
  const obj = {
    xmlData: xmlData.value,
    categoryId: props.info.id,
  }
  apiRequestPost('/bpm-service/BpmProcessDef/saveProcessImport', obj)
    .then((res) => {
      if (res.data.success) {
        emits('save-ok', res.data)
      } else {
      }
    })
    .catch((error: any) => {
      // 处理错误
    })
}
</script>
